//
// Created by Administrator on 2021/5/5.
//
/*

给你一个 m x n 的矩阵 board ，由若干字符 'X' 和 'O' ，找到所有被 'X' 围绕的区域，并将这些区域里所有的'O' 用 'X' 填充。
输入：board = [["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]]
输出：[["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]]
解释：被围绕的区间不会存在于边界上，换句话说，任何边界上的'O'都不会被填充为'X'。
任何不在边界上，或不与边界上的'O'相连的'O'最终都会被填充为'X'。如果两个元素在水平或垂直方向相邻，则称它们是“相连”的。

来源：力扣（LeetCode）
链接：https://leetcode-cn.com/problems/surrounded-regions
著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。

*/
#include <vector>
#include <iostream>
#include <queue>
using namespace std;

class Solution { // dfs
public:
    int n, m;

    void dfs(vector<vector<char>> &board, int x, int y) {
        // 递归函数，给所有与边缘联通的'0'打上标记
        if (x < 0 || x >= n || y < 0 || y >= m || board[x][y] != 'O') {
            return;
        }
        board[x][y] = 'A';
        dfs(board, x + 1, y);
        dfs(board, x - 1, y);
        dfs(board, x, y + 1);
        dfs(board, x, y - 1);
    }

    void solve(vector<vector<char>> &board) {
        n = (int) board.size(); // 行数
        if (n == 0) return; // 如果只有一行，肯定都在边缘
        m = (int) board[0].size(); // 列数
        // 两个for 循环，检查所有在周围的'0'
        for (int i = 0; i < n; i++) {  // 检查每一行的第一个和最后一个
            dfs(board, i, 0);
            dfs(board, i, m - 1);
        }
        for (int i = 1; i < m - 1; i++) { // 检查除首尾外 每一列的第一行和最后一行
            dfs(board, 0, i);
            dfs(board, n - 1, i);
        }
        // dfs 结束后，如果某个'0'被标记为A，说明它与边缘间接联通，就不可能被'X'包围
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (board[i][j] == 'A') {
                    board[i][j] = 'O';
                } else if (board[i][j] == 'O') {
                    board[i][j] = 'X'; // 被'X'包围的 打上'X'
                }
            }
        }
    }
};

class Solution2 { //bfs打标记
public:
    const int dx[4] = {1, -1, 0, 0};
    const int dy[4] = {0, 0, 1, -1};

    void solve(vector<vector<char>> &board) {
        int n = (int)board.size();
        if (n == 0) {
            return;
        }
        int m = (int)board[0].size();
        queue <pair<int, int>> que; // 队列
        for (int i = 0; i < n; i++) { // 搜索在第一列和最后一列的'0'
            if (board[i][0] == 'O') {
                que.emplace(i, 0);
            }
            if (board[i][m - 1] == 'O') {
                que.emplace(i, m - 1);
            }
        }
        for (int i = 1; i < m - 1; i++) { // 搜索在第一行和最后一行的'0'
            if (board[0][i] == 'O') {
                que.emplace(0, i);
            }
            if (board[n - 1][i] == 'O') {
                que.emplace(n - 1, i);
            }
        }
        while (!que.empty()) {
            int x = que.front().first, y = que.front().second;
            que.pop();
            board[x][y] = 'A';
            for (int i = 0; i < 4; i++) {
                int mx = x + dx[i], my = y + dy[i];
                if (mx < 0 || my < 0 || mx >= n || my >= m || board[mx][my] != 'O') {
                    continue;
                }
                que.emplace(mx, my); // 相邻的'0'加入队
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (board[i][j] == 'A') {
                    board[i][j] = 'O';
                } else if (board[i][j] == 'O') {
                    board[i][j] = 'X';
                }
            }
        }
    }
};


void printBoard(vector<vector<char>> &board) {
    for (const auto &row:board) {
        for (auto col:row) {
            cout << col << " ";
        }
        cout << endl;
    }
    cout << endl;
}

int main() {
    vector<vector<char>> board{{'X', 'X', 'X', 'X'},
                               {'X', 'O', 'O', 'X'},
                               {'X', 'X', 'O', 'X'},
                               {'X', 'O', 'X', 'X'}};
    Solution2 sol;
    printBoard(board);
    sol.solve(board);
    printBoard(board);
    return 0;
}